package com.mark.springsecurity.dto.resp;

import com.mark.springsecurity.domain.SysRole;
import com.mark.springsecurity.dto.req.vo.SysAuthorityVo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Page;

import java.util.Set;
import java.util.stream.Collectors;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class SysRoleDto {

    private Long id;

    private String name;

    private Set<SysAuthorityVo> authorities;

    public SysRoleDto(SysRole sysRole) {
        this.id = sysRole.getId();
        this.name = sysRole.getName();
        this.authorities = sysRole.getAuthorities().stream().map(SysAuthorityVo::build).collect(Collectors.toSet());
    }

    public static SysRoleDto build(SysRole sysRole) {
        return SysRoleDto.builder()
                .id(sysRole.getId())
                .name(sysRole.getName())
                .authorities(sysRole.getAuthorities().stream().map(SysAuthorityVo::build).collect(Collectors.toSet()))
                .build();
    }

    public static Page<SysRoleDto> buildListWithPage(Page<SysRole> sysRoles) {
        return sysRoles.map(SysRoleDto::new);
    }
}
